// pages/team/start-team.js
const util = require('../../utils/util.js');
const api = require('../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 总条数
    allCount: 0,
    // 总页数
    allPage: 0,
    // 开始页数
    page: 1,
    // 请求的条数
    size: 8,
    // 是否还有更多
    showTips: 0,
    // 正在开团的列表
    teamOrder: [],
    // 加载图标的大小
    sysHeight: 0,
    // 控制加载
    loading: 0,
  },
  onLoad: function (options) {

  },
  onShow: function () {
    // 获取设备信息
    let info = wx.getSystemInfoSync();
    let sysHeight = info.windowHeight - 100;
    this.setData({
      sysHeight: sysHeight,
      autoplay: true,
      page:1,
      teamOrder:[],
      loading:0
    });
    // 获取正在开团的团单列表
    this.getTeamOrder();
  },
  // 获取正在开团的团长列表
  getTeamOrder() {
    let that = this;
    util.requestApi(api.getTeamOrder, {
      page: that.data.page,
      size: that.data.size
    }).then(function (res) {
      if (res.code == 200) {
        let orderList=that.data.teamOrder;
        that.setData({
          teamOrder: orderList.concat(res.content.data),
          loading: 1,
          allCount: res.content.count,
          allPage: Math.ceil(res.content.count / that.data.size),
          showTips: res.content.data.length < that.data.size ? 1 : 0
        })
      }
    })
  },
  goGoods: function (e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../goods-team/goods-team?id=' + id,
    })
  },
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    this.setData({
      loading: 0,
      allCount: 0,
      allPage: 0,
      page: 1,
      teamOrder: []
    })
    this.getTeamOrder();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  // 页面上拉触底事件的处理函数
  onReachBottom: function () {
    let that = this;
    if (that.data.page >= that.data.allPage) {
      that.setData({
        showTips: 1
      });
      return false;
    }
    that.setData({
      page: that.data.page + 1
    });
    that.getTeamOrder();
  },
  onShareAppMessage: function () {
  }
})